package com.woniu.inlet.web;

import com.woniu.outlet.po.Associates;
import com.woniu.outlet.po.Company;
import com.woniu.outlet.po.CompanyExtend;
import com.woniu.outlet.po.CompanyKeyPerson;
import com.woniu.service.ICompanyService;
import com.woniu.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/*
主要处理办业务的问题（转变为正式的客户！！！）：即从潜在的管户客户转变为办理具体业务的客户，需要添加company表，companyextend表，关联企业表associate
联系人表companykeyperson表，再添加完的同时删除删除潜在客户表的这条信息，成为正式的客户，并且发送一条短信给客户！！
 */
@RestController
@RequestMapping("/cli")
@Api(tags = "正式客户的信息处理")
public class CompanyController {

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    //在正式添加之前，我们需要使用到幂等性的一些解决方案
    @ApiOperation(value = "createregularcustomerredis",notes = "正式添加客户之前的幂等性")
    @GetMapping("/createregularcustomerredis")
    @PreAuthorize("hasRole('client')")
    public ResponseResult<String> createregularcustomerredis(){
        //在执行这个方法的时候，在redis里面生成一个redis
        redisTemplate.opsForValue().set("createregularcustomerredis", "createregularcustomerredis");
        return new ResponseResult<>("开始添加正式客户！！",200);
    }


    //添加正式客户的信息
    @ApiOperation(value = "addregularcustomer",notes = "添加正式客户的信息")
    @PostMapping("/addregularcustomer")
    @PreAuthorize("hasRole('client')")
    public ResponseResult<String> addregularcustomer(@RequestBody Company company){
        //在这里实现幂等性的方法，只能注册一次，判断redis里面是否有这个键值
        String createregularcustomerredis = redisTemplate.opsForValue().get("createregularcustomerredis");
        if(createregularcustomerredis==null){
            //说明已经操作过一次了，所以不能在操作了
            return new ResponseResult<>("你已经注册过了，请不要频繁操作！！",200);
        }
        int addregularcustomer = companyService.addregularcustomer(company);
        return new ResponseResult<>("正式客户添加成功！！",200);
    }


    //修改企业客户表（主表：company）的具体信息
    @ApiOperation(value = "updatecompany",notes = "修改企业客户表（主表：company）的具体信息")
    @PostMapping("/updatecompany")
    @PreAuthorize("hasRole('client')")
    public ResponseResult<String> updatecompany(@RequestBody Company company){
        int updatecompany = companyService.updatecompany(company);
        return new ResponseResult<>("修改company成功！！",200);
    }

    //修改企业的扩展表companyextend
    @ApiOperation(value = "updatecompanyextend",notes = "修改企业的扩展表companyextend")
    @PostMapping("/updatecompanyextend")
    @PreAuthorize("hasRole('client')")
    public ResponseResult<String> updatecompanyextend(@RequestBody CompanyExtend companyExtend){
        int updatecompanyextend = companyService.updatecompanyextend(companyExtend);
        return new ResponseResult<>("修改companyextend成功！！",200);
    }

    //修改联系人的信息
    @ApiOperation(value = "updatecompanykeyperson",notes = "修改联系人的信息")
    @PostMapping("/updatecompanykeyperson")
    @PreAuthorize("hasRole('client')")
    public ResponseResult<String> updatecompanykeyperson(@RequestBody CompanyKeyPerson companyKeyPerson){
        int updatecompanykeyperson = companyService.updatecompanykeyperson(companyKeyPerson);
        return new ResponseResult<>("修改companykeyperson成功！！",200);
    }

    //修改关联企业的信息
    @ApiOperation(value = "updateassociate",notes = "修改关联企业的信息")
    @PostMapping("/updateassociate")
    @PreAuthorize("hasRole('client')")
    public ResponseResult<String> updateassociate(@RequestBody Associates associates){
        int updateassociate = companyService.updateassociate(associates);
        return new ResponseResult<>("修改associate成功！！",200);
    }

    //添加关联企业的信息，一般而言一个企业的关联企业是一对多的关系
    @ApiOperation(value = "addassociate",notes = "添加关联企业的信息")
    @PostMapping("/addassociate")
    @PreAuthorize("hasRole('client')")
    public ResponseResult<String> addassociate(@RequestBody Associates associates){
        int addassociate = companyService.addassociate(associates);
        return new ResponseResult<>("添加关联企业成功！！",200);
    }


}
